<template>
    <div>
        <h1>用户管理</h1>
        <div>
            <el-form
                :inline="true"
                :model="searchForm"
                class="demo-form-inline"
            >
                <el-form-item label="关键字">
                    <el-input
                        v-model="searchForm.keyWord"
                        placeholder="请输入查询关键字"
                        clearable
                    />
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="onQuery">查询</el-button>
                    <el-button
                        type="danger"
                        @click="onDeleteAll"
                        :disabled="multipleSelection.length == 0"
                        >批量删除</el-button
                    >
                    <el-button type="success" @click="onExportUserAll"
                        >批量导出</el-button
                    >
                </el-form-item>
            </el-form>
        </div>
        <div>
            <el-upload
                ref="uploadRef"
                class="upload-demo"
                action="http://localhost:10010/user/index/importUser"
                :headers="headers"
                :auto-upload="false"
                :on-success="handleAvatarSuccess"
            >
                <template #trigger>
                    <el-button type="primary">选择导入的文件</el-button>
                </template>

                <el-button class="ml-3" type="success" @click="submitUpload">
                    开始导入
                </el-button>
            </el-upload>
        </div>
        <div>
            <el-table
                :data="tableData"
                stripe
                style="width: 100%"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" />
                <el-table-column prop="userId" label="ID" width="80" />
                <el-table-column prop="userName" label="登录账号" width="140" />
                <el-table-column prop="nickName" label="真实姓名" width="120" />
                <el-table-column prop="userMobile" label="手机号" width="120" />
                <el-table-column prop="userEmail" label="邮箱" width="180" />
                <el-table-column prop="userAvatar" label="头像" width="180">
                    <template #default="scope">
                        <img
                            v-if="scope.row.userAvatar != null"
                            :src="scope.row.userAvatar"
                            alt=""
                            style="width: 50px; height: 50px"
                        />
                    </template>
                </el-table-column>
                <el-table-column prop="userStatus" label="状态" width="140">
                    <template #default="scope">
                        <el-tag v-if="scope.row.userStatus == 0">待审核</el-tag>
                        <el-tag type="success" v-if="scope.row.userStatus == 1"
                            >审核通过</el-tag
                        >
                        <el-tag type="danger" v-if="scope.row.userStatus == 2"
                            >审核驳回</el-tag
                        >
                    </template>
                </el-table-column>
                <el-table-column prop="reason" label="审核说明" width="180" />
                <el-table-column
                    prop="createTime"
                    label="注册时间"
                    width="180"
                />
                <el-table-column label="操作">
                    <template #default="scope">
                        <el-button
                            type="primary"
                            size="small"
                            v-if="scope.row.userStatus == 0"
                            @click="onCheckUser(scope.row)"
                            >审核</el-button
                        >
                        <el-button
                            type="danger"
                            size="small"
                            @click="onDeleteUser(scope.row)"
                            >删除</el-button
                        >
                        <el-button
                            type="success"
                            size="small"
                            @click="onCheckUser(scope.row)"
                            >修改</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div style="margin-top: 20px">
            <el-pagination
                v-model:current-page="searchForm.pageNum"
                v-model:page-size="searchForm.pageSize"
                :page-sizes="[2, 4, 6, 8]"
                :background="true"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
            />
        </div>
        <div>
            <el-dialog v-model="dialogFormVisible" title="商户审核" width="500">
                <el-form :model="form">
                    <el-form-item
                        label="商户账号"
                        :label-width="formLabelWidth"
                    >
                        <el-input
                            v-model="form.userName"
                            autocomplete="off"
                            disabled
                        />
                    </el-form-item>
                    <el-form-item
                        label="商户姓名"
                        :label-width="formLabelWidth"
                    >
                        <el-input
                            v-model="form.nickName"
                            autocomplete="off"
                            disabled
                        />
                    </el-form-item>
                    <el-form-item label="手机号" :label-width="formLabelWidth">
                        <el-input
                            v-model="form.userMobile"
                            autocomplete="off"
                            disabled
                        />
                    </el-form-item>
                    <el-form-item label="邮箱" :label-width="formLabelWidth">
                        <el-input
                            v-model="form.userEmail"
                            autocomplete="off"
                            disabled
                        />
                    </el-form-item>
                    <el-form-item label="审核" :label-width="formLabelWidth">
                        <el-radio-group v-model="form.userStatus">
                            <el-radio :value="1">通过</el-radio>
                            <el-radio :value="2">驳回</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item
                        label="审核说明"
                        :label-width="formLabelWidth"
                    >
                        <el-input
                            type="textarea"
                            v-model="form.reason"
                            autocomplete="off"
                        />
                    </el-form-item>
                </el-form>
                <template #footer>
                    <div class="dialog-footer">
                        <el-button @click="dialogFormVisible = false"
                            >放弃</el-button
                        >
                        <el-button type="primary" @click="onSubmitCheckUser">
                            提交
                        </el-button>
                    </div>
                </template>
            </el-dialog>
        </div>
    </div>
</template>

<script setup lang="ts">
import { getUserList, checkUser, deleteUser, deleteUserAll } from "@/api/api";
import { ElMessage, ElMessageBox } from "element-plus";
import { onMounted, ref } from "vue";
import type { UploadInstance } from 'element-plus'
import type { UploadProps } from 'element-plus'


const searchForm = ref({
    pageNum: 1,
    pageSize: 2,
    keyWord: null,
});
const total = ref(0);
const tableData = ref([]);

const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
const form = ref({
    userId: null,
    userStatus: null,
    reason: null,
    userName: null,
    nickName: null,
    userMobile: null,
    userEmail: null,
});

const multipleSelection = ref<any[]>([]);

const headers = { token: sessionStorage.getItem("token") };
const uploadRef = ref<UploadInstance>()

const handleAvatarSuccess: UploadProps['onSuccess'] = (
  response,
  uploadFile
) => {
  console.log('handleAvatarSuccess', response);
  if(response.code == 200) {
    ElMessage.success('导入成功');
    loadList();
  } else {
    ElMessage.error(response.msg);
  }
  
}

const submitUpload = () => {
  uploadRef.value!.submit()
}


// 批量导出用户
const onExportUserAll = () => {
    console.log("onExportUserAll");
    let url = "http://localhost:9001/user/index/exportAllUser";
    window.open(url);
};

const handleSelectionChange = (val: any[]) => {
    multipleSelection.value = val;
    console.log("multipleSelection", multipleSelection.value);
};

// 批量删除
const onDeleteAll = () => {
    console.log("onDeleteAll");

    ElMessageBox.confirm("删除数据后,无法恢复,您确认要删除吗?", "警告", {
        confirmButtonText: "确认",
        cancelButtonText: "放弃",
        type: "warning",
    })
        .then(() => {
            // 确认
            deleteUserAll(multipleSelection.value).then((res) => {
                console.log("res", res);
                if (res.data.code == 200) {
                    ElMessage.success("删除成功");
                    loadList();
                } else {
                    ElMessage.error(res.data.msg);
                }
            });
        })
        .catch(() => {
            // 放弃
        });
};

// 删除商户
const onDeleteUser = (row: any) => {
    console.log("onDeleteUser", row);
    ElMessageBox.confirm("数据删除后无法恢复,您确认要删除吗?", "警告", {
        confirmButtonText: "确认",
        cancelButtonText: "放弃",
        type: "warning",
    })
        .then(() => {
            // 确认删除
            deleteUser(row.userId).then((res) => {
                if (res.data.code == 200) {
                    ElMessage.success("删除成功");
                    loadList();
                } else {
                    ElMessage.error(res.data.msg);
                }
            });
        })
        .catch(() => {
            // 放弃删除
        });
};

// 提交审核
const onSubmitCheckUser = () => {
    checkUser(form.value).then((res) => {
        if (res.data.code == 200) {
            dialogFormVisible.value = false;
            ElMessage.success("提交成功");
            loadList();
        } else {
            ElMessage.error(res.data.msg);
        }
    });
};

// 审核用户
const onCheckUser = (row: any) => {
    console.log("row", row);
    //form.value = row;
    Object.assign(form.value, row);
    dialogFormVisible.value = true;
};

// 搜索
const onQuery = () => {
    loadList();
};

const handleSizeChange = (val: number) => {
    console.log(`${val} items per page`);
    searchForm.value.pageSize = val;
    loadList();
};
const handleCurrentChange = (val: number) => {
    console.log(`current page: ${val}`);
    searchForm.value.pageNum = val;
    loadList();
};

// 获取用户列表
const loadList = () => {
    getUserList(searchForm.value).then((res) => {
        console.log("res", res);
        if (res.data.code == 200) {
            tableData.value = res.data.data.records;
            total.value = res.data.data.total;
        }
    });
};

// 页面钩子函数
onMounted(() => {
    loadList();
});
</script>

<style scoped></style>
